var t = require("../@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var e, o = require("../@babel/runtime/helpers/defineProperty"), a = t(require("../http/address")), d = t(require("../http/order")), i = require("../utils/tool"), n = new a.default(), s = new d.default(), r = require("../http/indexApi.js"), c = Behavior({
    data: (e = {
        productId: "",
        sizeId: "",
        show: !1
    }, o(e, "sizeId", ""), o(e, "orderType", 0), o(e, "quantity", 1), o(e, "discountType", 0), 
    o(e, "couponHistoryId", ""), o(e, "addressId", ""), o(e, "couponCount", ""), o(e, "integralTotal", 0), 
    o(e, "integralUsable", 0), o(e, "integralToMoney", 0), e),
    attached: function() {},
    methods: {
        getDefaultAddressId: function() {
            var t = this;
            wx.showLoading({
                mask: !0,
                title: "加载中…"
            }), n.getDefaultAddress().then(function(e) {
                t.setData({
                    addressId: e.data.addressId || ""
                }), t.buy();
            }).catch(function(t) {
                (0, i.toastToPage)(t.msg), wx.hideLoading();
            });
        },
        buy: function() {
            if (!this.data.addressId) return wx.hideLoading(), void this.triggerEvent("showDeliver", {});
            0 == this.data.discountPrice ? this.zeroPay() : this.normalPay();
        },
        zeroPay: function() {
            var t = this;
            s.createZeroBlindOrder({
                productId: this.data.productId,
                productSpecId: this.data.sizeId,
                orderType: 0,
                quantity: this.data.quantity,
                discountType: this.data.discountType,
                couponHistoryId: this.data.couponHistoryId,
                addressId: this.data.addressId
            }).then(function(e) {
                wx.hideLoading(), t.onClose(), t.orderId = e.data.orderId, t.successNews();
            }).catch(function(t) {
                (0, i.toastToPage)(t.msg), wx.hideLoading();
            });
        },
        normalPay: function() {
            var t = this;
            s.createBlindOrder({
                productId: this.data.productId,
                productSpecId: this.data.sizeId,
                orderType: 0,
                quantity: this.data.quantity,
                discountType: this.data.discountType,
                couponHistoryId: this.data.couponHistoryId,
                addressId: this.data.addressId
            }).then(function(e) {
                t.orderId = e.data.orderId, t.getCode(e.data.orderId);
            }).catch(function(t) {
                (0, i.toastToPage)(t.msg), wx.hideLoading();
            });
        },
        getCode: function(t) {
            var e = this;
            wx.login({
                success: function(o) {
                    o.code ? e.getPayInfo(o.code, t) : ((0, i.toastToPage)("获取code失败" + o.errMsg), wx.hideLoading());
                }
            });
        },
        getPayInfo: function(t, e) {
            var o = this;
            s.getWxPayInfo(t, e).then(function(t) {
                (0, i.callWeChatPay)(t.data, function(t) {
                    wx.hideLoading(), o.onClose(), o.successNews();
                }, function(t) {
                    o.cancelOrder(e);
                });
            }).catch(function(t) {
                (0, i.toastToPage)(t.msg), wx.hideLoading();
            });
        },
        cancelOrder: function(t) {
            s.cancelOrder(t).then(function(t) {
                wx.hideLoading(), (0, i.toastToPage)("订单已取消");
            }).catch(function(t) {
                (0, i.toastToPage)(t.msg), wx.hideLoading();
            });
        },
        successNews: function() {
            var t = this, e = this;
            wx.requestSubscribeMessage({
                tmplIds: [ "vBPr22DDXOhYMu7o3hc3k055YKUvYSmncOQjkcVzLE0", "hdkwX1PFKY08Wrw3z18S6oFpn2SeoyToVW4qGW2L-4w", "C1t5mpxs6FRapLx2Ht1gnmwpDyKwJj1HkEzWJ_mv6R0" ],
                success: function(t) {
                    r.subscribeNotice({
                        messageType: 7,
                        messageId: e.orderId
                    }).then(function(t) {}).catch(function(t) {});
                },
                fail: function(t) {
                    console.log(t);
                },
                complete: function(e) {
                    t.successDialog();
                }
            });
        },
        showPopup: function() {
            this.setData({
                show: !0
            }), 0 == this.properties.info.productSpecListVo.length ? this.computedPrice() : this.getIntegralTotal();
        },
        onClose: function() {
            this.setData({
                show: !1
            }), this.triggerEvent("hideBuy", {});
        },
        successDialog: function() {
            this.selectComponent("#success").showPopup();
        },
        computedPrice: function() {
            var t = this;
            s.getPreviewPrice({
                productId: this.data.productId,
                orderType: 0,
                quantity: this.data.quantity,
                discountType: this.data.discountType,
                couponHistoryId: this.data.couponHistoryId,
                addressId: this.data.addressId
            }).then(function(e) {
                t.setData({
                    couponCount: e.data.couponCount,
                    integralTotal: e.data.integrationExchangeDto.integration,
                    integralUsable: e.data.sureIntegrationNum,
                    integralToMoney: e.data.sureIntegrationMoney,
                    couponToMoney: e.data.couponContent.deductionMoney,
                    discountPrice: e.data.discountPrice
                });
            }).catch(function(t) {
                (0, i.toastToPage)(t.msg);
            });
        }
    }
});

exports.default = c;